package com.addplus.townmall.server.api.mapper.town;

import com.addplus.townmall.server.api.model.town.ShopProductFight;
import com.addplus.townmall.server.api.model.town.expand.ShopProductDescribeExpand;
import com.addplus.townmall.server.api.model.town.expand.ShopProductExpand;
import com.addplus.townmall.server.api.model.town.expand.ShopProductFightExpand;
import com.addplus.townmall.server.api.model.town.expand.SolrModelExpand;
import com.addplus.townmall.server.api.model.town.param.AddShopProductFightParam;
import com.addplus.townmall.server.api.model.town.param.OrderItemsFightParam;
import com.addplus.townmall.server.api.model.town.param.OrderShopItemsParam;
import com.addplus.townmall.server.api.utils.BaseAddMapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 拼团商品信息表 Mapper 接口
 * </p>
 *
 * @author zhangjiehang
 * @since 2018-10-30
 */
public interface ShopProductFightMapper extends BaseAddMapper<ShopProductFight> {
    @Select("select s.*, sd.shop_product_describe from shop_product_fight s left join shop_product_describe_fight as sd on s.id = sd.shop_product_id where s.id = #{id}")
    AddShopProductFightParam getShopProductById(Integer id);


    @Select("SELECT\n" +
            " s.id,\n" +
            " s.shop_code,\n" +
            " s.shop_name,\n" +
            " s.shop_share_describe,\n" +
            " s.shop_describe,\n" +
            " s.shop_imgs,\n" +
            " s.shop_seal_img,\n" +
            " s.store_id,\n" +
            " s.shop_category_id,\n" +
            " s.shop_video,\n" +
            " s.sale_price,\n" +
            " s.line_price,\n" +
            " s.fight_price,\n" +
            " s.group_time,\n" +
            " s.group_num,\n" +
            " s.is_only,\n" +
            " s.is_more,\n" +
            " s.unit,\n" +
            " s.buy_limit,\n" +
            " s.one_buy_limit,\n" +
            " s.online_time,\n" +
            " s.stock,\n" +
            " s.shop_customize_code,\n" +
            " s.sale_status,\n" +
            " s.online_status,\n" +
            " s.visit_count+s.vistit_virtual_count as visit_count,\n" +
            " s.sale_count+s.sale_virtual_count as sale_count,\n" +
            " s.shop_basic_gurarantee_path\n" +
            "FROM shop_product_fight s WHERE  s.audit_status =0 AND s.is_deleted=0 AND s.id = #{shopProductFightId}")
    @Results({
            @Result(column = "id", property = "shopProductFight.id"),
            @Result(column = "shop_code", property = "shopProductFight.shopCode"),
            @Result(column = "shop_name", property = "shopProductFight.shopName"),
            @Result(column = "shop_share_describe", property = "shopProductFight.shopShareDescribe"),
            @Result(column = "shop_describe", property = "shopProductFight.shopDescribe"),
            @Result(column = "shop_imgs", property = "shopProductFight.shopImgs"),
            @Result(column = "shop_seal_img", property = "shopProductFight.shopSealImg"),
            @Result(column = "store_id", property = "shopProductFight.storeId"),
            @Result(column = "shop_category_id", property = "shopProductFight.shopCategoryId"),
            @Result(column = "shop_describe", property = "shopProductFight.shopDescribe"),
            @Result(column = "shop_video", property = "shopProductFight.shopVideo"),
            @Result(column = "sale_price", property = "shopProductFight.salePrice"),
            @Result(column = "line_price", property = "shopProductFight.linePrice"),
            @Result(column = "fight_price", property = "shopProductFight.fightPrice"),
            @Result(column = "group_time", property = "shopProductFight.groupTime"),
            @Result(column = "group_num", property = "shopProductFight.groupNum"),
            @Result(column = "group_num", property = "shopProductFight.groupNum"),
            @Result(column = "is_only", property = "shopProductFight.isOnly"),
            @Result(column = "is_more", property = "shopProductFight.isMore"),
            @Result(column = "buy_limit", property = "shopProductFight.buyLimit"),
            @Result(column = "unit", property = "shopProductFight.unit"),
            @Result(column = "one_buy_limit", property = "shopProductFight.oneBuyLimit"),
            @Result(column = "online_time", property = "shopProductFight.onlineTime"),
            @Result(column = "stock", property = "shopProductFight.stock"),
            @Result(column = "shop_customize_code", property = "shopProductFight.shopCustomizeCode"),
            @Result(column = "sale_status", property = "shopProductFight.saleStatus"),
            @Result(column = "online_status", property = "shopProductFight.onlineStatus"),
            @Result(column = "visit_count", property = "shopProductFight.visitCount"),
            @Result(column = "sale_count", property = "shopProductFight.saleCount"),
            @Result(column = "shop_basic_gurarantee_path", property = "shopProductFight.shopBasicGuraranteePath")
    })
    ShopProductDescribeExpand getShopProductFightContext(@Param("shopProductFightId") Integer shopProductFightId);

    @Select("SELECT p.store_id, p.id as shop_product_id,p.shop_name,p.shop_seal_img,p.shop_fright_id,p.fright_type,p.fright,p.online_status,\n" +
            "p.shop_customize_code,sku.id as shop_specification_sku_id," +
            "p.group_time,p.group_num,p.is_only,p.is_more,p.colonel,p.buy_limit,p.one_buy_limit," +
            "sku.sku_describe as shop_specification_sku_json," +
            "sku.sale_price,sku.cost_price,sku.discount_price,sku.line_price,sku.stock,p.shop_tag\n" +
            "FROM shop_product_fight p \n" +
            "LEFT JOIN shop_specification_sku sku ON sku.shop_product_id = p.id AND sku.is_deleted = 0\n" +
            "WHERE \n" +
            "sku.id = #{shopSpecificationSkuId} " +
            "AND p.audit_status = 0 AND p.is_deleted = 0 AND p.sale_status = 0 " +
            "AND sku.type = 2")
    OrderItemsFightParam getShopProductFightOrderMessage(@Param("shopSpecificationSkuId") Integer shopSpecificationSkuId);

    @Select("select spf.*,s.store_name " +
            "from floor_shop fs " +
            "inner JOIN shop_product_fight spf ON fs.shop_id = spf.id " +
            "inner JOIN store s ON spf.store_id = s.id "+
            "where ${ew.sqlSegment}")
    List<ShopProductFightExpand> getFloorShopProductFight(@Param("ew") Wrapper wrapper);

    @Select("select spf.*,s.store_name " +
            "from shop_product_fight spf " +
            "inner JOIN store s ON spf.store_id = s.id "+
            "where ${ew.sqlSegment}")
    List<ShopProductFightExpand> getShopProductFightByQry(Page page, @Param("ew") Wrapper wrapper);

    @Select("select * from shop_product_fight pf where pf.shop_code =#{shopcode}")
    ShopProductFightExpand getShopProductFightByShopCode(@Param("shopcode") String code);


    @Select("SELECT\n" +
            " f.id,\n" +
            " f.online_status, \n" +
            " f.limit_time \n" +
            "FROM\n" +
            " shop_product_fight f\n" +
            " LEFT JOIN order_items_fight o ON o.shop_product_id = f.id \n" +
            " AND o.is_delete = 0 \n" +
            "WHERE\n" +
            " o.order_id = #{orderId} \n" +
            " AND f.is_deleted = 0")
    ShopProductFight getShopProductByOrderFightId(@Param("orderId") Integer orderId);

    @Select("SELECT\n" +
            "\tp.id,\n" +
            "\tp.shop_name,\n" +
            "\tp.shop_describe,\n" +
            "\tp.shop_seal_img,\n" +
            "\tp.shop_seal_img_pc,\n" +
            "\tp.store_id,\n" +
            "\tp.shop_category_id,\n" +
            "\tp.shop_tag,\n" +
            "\tp.sale_price,\n" +
            "\tp.line_price,\n" +
            "\tp.online_status,\n" +
            "\tp.collect_virtual_count,\n" +
            "\tp.collect_count,\n" +
            "\tp.visit_count,\n" +
            "\tp.vistit_virtual_count,\n" +
            "\tp.sale_count,\n" +
            "\tp.sale_virtual_count,\n" +
            "\tp.is_deleted,\n" +
            "\tp.gmt_create,\n" +
            "\td.shop_product_describe,\n" +
            "\td.shop_goods,\n" +
            "\td.shop_comment_count,\n" +
            "\td.shop_product_overall_rate,\n" +
            "\td.shop_product_rate,\n" +
            "\td.shop_product_service_rate,\n" +
            "\td.shop_product_logistics_rate,\n" +
            "\ta.total_comment,\n" +
            "\ta.good_comment,\n" +
            "\ta.middle_comment,\n" +
            "\ta.bad_comment,\n" +
            "\ta.good_rate \n" +
            "FROM\n" +
            "\tshop_product_fight p\n" +
            "\tLEFT JOIN shop_product_describe_fight d ON d.shop_product_id = p.id \n" +
            "\tAND d.is_deleted = 0\n" +
            "\tLEFT JOIN shop_comment_all a ON a.shop_product_id = p.id \n" +
            "\tAND a.is_deleted = 0 \n" +
            "\tAND a.type = 2 \n" +
            "WHERE\n" +
            "\tp.id = #{id}\t\n" +
            "\tAND p.is_deleted = 0")
    SolrModelExpand getSolrModel(@Param("id") Integer id);

}
